//#define __DEBUG__
// by Xeno
#define THIS_FILE "fn_take_backpack.sqf"
#include "x_setup.sqf"

#include "x_uiinc.sqf"

private ["_control", "_sel", "_bar", "_typeold", "_oldbpobj", "_dispname"];
disableSerialization;

_control = __uiGetVar(GVAR(BACKPACK_DIALOG)) displayCtrl 1000;

_sel = lbCurSel _control;

if (_sel == -1) exitWith {};

_bar = switch (GVAR(side_player)) do {
	case blufor: {GVAR(backpackclasses) select 0};
	case opfor: {GVAR(backpackclasses) select 1};
};

_typeold = "";
_oldbpobj = unitBackpack player;

if (!isNull _oldbpobj) then {
	_typeold = typeOf _oldbpobj;
	removeBackpack  player;
};

player addBackpack (_bar select _sel);

_dispname = getText (configFile/"cfgVehicles"/(_bar select _sel)/"displayName");

systemChat format [(localize "STR_DOM_MISSIONSTRING_688"), _dispname];

clearWeaponCargo (unitBackpack player);
clearItemCargo (unitBackpack player);
clearMagazineCargo (unitBackpack player);

[QGVAR(p_o_a2), [GVAR(string_player), unitBackpack player]] call FUNC(NetCallEventCTS);

_bar = _bar - [_bar select _sel];

if (_typeold != "" && {!(_typeold in _bar)}) then {
	_bar set [count _bar, _typeold];
};

switch (GVAR(side_player)) do {
	case blufor: {GVAR(backpackclasses) set [0, _bar]};
	case opfor: {GVAR(backpackclasses) set [1, _bar]};
};

if (!isNull _oldbpobj) then {
	[QGVAR(p_o_a2r), [GVAR(string_player), _oldbpobj]] call FUNC(NetCallEventCTS);
};